from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QPushButton, QLabel, QGroupBox,
    QMessageBox, QFileDialog, QDialog, QDialogButtonBox
)
from PyQt6.QtGui import QFont
from PyQt6.QtCore import Qt

class DialogsTab:
    """对话框标签页"""
    
    def __init__(self, parent=None, status_bar=None):
        self.parent = parent
        self.status_bar = status_bar
        
    def create_tab(self):
        """创建对话框标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        
        title = QLabel("对话框使用示例")
        title.setFont(QFont("Arial", 16, QFont.Weight.Bold))
        title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title)
        
        # 信息对话框
        info_group = QGroupBox("信息对话框")
        info_layout = QVBoxLayout(info_group)
        
        info_btn = QPushButton("显示信息对话框")
        info_btn.clicked.connect(self.show_info_dialog)
        info_layout.addWidget(info_btn)
        layout.addWidget(info_group)
        
        # 警告对话框
        warning_group = QGroupBox("警告对话框")
        warning_layout = QVBoxLayout(warning_group)
        
        warning_btn = QPushButton("显示警告对话框")
        warning_btn.clicked.connect(self.show_warning_dialog)
        warning_layout.addWidget(warning_btn)
        layout.addWidget(warning_group)
        
        # 问题对话框
        question_group = QGroupBox("问题对话框")
        question_layout = QVBoxLayout(question_group)
        
        question_btn = QPushButton("显示问题对话框")
        question_btn.clicked.connect(self.show_question_dialog)
        question_layout.addWidget(question_btn)
        layout.addWidget(question_group)
        
        # 文件对话框
        file_group = QGroupBox("文件对话框")
        file_layout = QVBoxLayout(file_group)
        
        open_btn = QPushButton("打开文件")
        open_btn.clicked.connect(self.open_file_dialog)
        
        save_btn = QPushButton("保存文件")
        save_btn.clicked.connect(self.save_file_dialog)
        
        file_layout.addWidget(open_btn)
        file_layout.addWidget(save_btn)
        layout.addWidget(file_group)
        
        # 自定义对话框
        custom_group = QGroupBox("自定义对话框")
        custom_layout = QVBoxLayout(custom_group)
        
        custom_btn = QPushButton("显示自定义对话框")
        custom_btn.clicked.connect(self.show_custom_dialog)
        custom_layout.addWidget(custom_btn)
        layout.addWidget(custom_group)
        
        return tab
    
    def show_info_dialog(self):
        """显示信息对话框"""
        QMessageBox.information(self.parent, "信息", "这是一个信息对话框示例")
    
    def show_warning_dialog(self):
        """显示警告对话框"""
        QMessageBox.warning(self.parent, "警告", "这是一个警告对话框示例")
    
    def show_question_dialog(self):
        """显示问题对话框"""
        reply = QMessageBox.question(
            self.parent, "问题", "你确定要继续吗?",
            QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
        )
        if reply == QMessageBox.StandardButton.Yes:
            if self.status_bar:
                self.status_bar.showMessage("用户选择了: 是", 3000)
        else:
            if self.status_bar:
                self.status_bar.showMessage("用户选择了: 否", 3000)
    
    def open_file_dialog(self):
        """打开文件对话框"""
        file_name, _ = QFileDialog.getOpenFileName(
            self.parent, "打开文件", "", "所有文件 (*);;文本文件 (*.txt);;图片 (*.png *.jpg)"
        )
        if file_name and self.status_bar:
            self.status_bar.showMessage(f"已选择文件: {file_name}", 5000)
    
    def save_file_dialog(self):
        """保存文件对话框"""
        file_name, _ = QFileDialog.getSaveFileName(
            self.parent, "保存文件", "", "所有文件 (*);;文本文件 (*.txt);;图片 (*.png *.jpg)"
        )
        if file_name and self.status_bar:
            self.status_bar.showMessage(f"文件将保存到: {file_name}", 5000)
    
    def show_custom_dialog(self):
        """显示自定义对话框"""
        dialog = QDialog(self.parent)
        dialog.setWindowTitle("自定义对话框")
        dialog.setFixedSize(300, 200)
        
        layout = QVBoxLayout(dialog)
        
        label = QLabel("这是一个自定义对话框示例")
        label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(label)
        
        btn_box = QDialogButtonBox(
            QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
        )
        btn_box.accepted.connect(dialog.accept)
        btn_box.rejected.connect(dialog.reject)
        layout.addWidget(btn_box)
        
        if dialog.exec() == QDialog.DialogCode.Accepted:
            if self.status_bar:
                self.status_bar.showMessage("自定义对话框: 用户选择了确定", 3000)
        else:
            if self.status_bar:
                self.status_bar.showMessage("自定义对话框: 用户选择了取消", 3000)